/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package biz;

import da.CommentJpaController;
import da.exceptions.PreexistingEntityException;
import da.exceptions.RollbackFailureException;
import entities.Comment;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.TypedQuery;
import javax.transaction.UserTransaction;

/**
 *
 * @author Quan Ho
 */
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class CommentProgess {

    @Resource
    private UserTransaction utx;
    @PersistenceUnit(unitName = "ProjectSem4PU")
    private EntityManagerFactory emf = null;
    private CommentJpaController jpa;

    public CommentJpaController getJpa() {
        if (jpa == null) {
            jpa = new CommentJpaController(utx, emf);
        }
        return jpa;
    }

    public void createComment(Comment cmt) throws PreexistingEntityException, RollbackFailureException, Exception {
        getJpa().create(cmt);
    }

    public List<Comment> getCommentByComplainId(int id) {

        TypedQuery<Comment> tq = emf.createEntityManager().createQuery("SELECT n FROM Comment n WHERE n.complainId.id = :id", Comment.class);
        tq.setParameter("id", id);
        try {
            return tq.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
}
